import { login, logout, geUserInfo } from "@/api/login";
import { getToken, setToken, removeToken } from "@/utils/auth";

const user = {
  state: {
    token: getToken(),
    name: "",
    avatar: "",
    roles: [],
    permissions: [],
    storeId: "",
    storeName: "",
    userId: 0,
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions;
    },
    SET_STORE_ID: (state, storeId) => {
      state.storeId = storeId;
    },
    SET_USER_ID: (state, userId) => {
      state.userId = userId;
    },
    SET_STORE_NAME: (state, storeName) => {
      state.storeName = storeName;
    },
  },

  actions: {
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      const username = userInfo.username.trim();
      const password = userInfo.password;
      const code = userInfo.code;
      const uuid = userInfo.uuid;
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid)
          .then((response) => {
            if (response.errno) {
              reject(response);
              return;
            }
            const isImproved = response.data.brandIsImproved;
            const token = response.data.sessionId;
            commit("SET_TOKEN", token);
            setToken(token);
            // setBrandIsImproved(isImproved);
            resolve(isImproved);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        geUserInfo()
          .then((res) => {
            let avatar = require("@/assets/images/avatar.png");
            if (res.data.avatar) {
              avatar = res.data.avatar;
            }
            commit("SET_AVATAR", avatar);
            if (res.data.roles?.length) {
              // 验证返回的roles是否是一个非空数组
              commit("SET_ROLES", res.data.roles);
              commit("SET_PERMISSIONS", res.data.perms);
            } else {
              commit("SET_ROLES", [""]);
            }
            commit("SET_NAME", res.data.name);
            commit("SET_STORE_ID", res.data.brandId);
            commit("SET_USER_ID", res.data.userId);
            commit("SET_STORE_NAME", res.data.brandName);
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token)
          .then(() => {
            commit("SET_TOKEN", "");
            commit("SET_ROLES", []);
            commit("SET_PERMISSIONS", []);
            commit("SET_STORE_ID", "");
            commit("SET_STORE_NAME", "");
            removeToken();
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    ClearUserInfo({ commit, state }) {
      commit("SET_TOKEN", "");
      commit("SET_ROLES", []);
      commit("SET_PERMISSIONS", []);
      commit("SET_STORE_ID", "");
      commit("SET_STORE_NAME", "");
      removeToken();
    },
    // 前端登出
    FedLogOut({ commit }) {
      return new Promise((resolve) => {
        commit("SET_TOKEN", "");
        removeToken();
        resolve();
      });
    },
  },
};

export default user;
